<?php
include_once dirname(__FILE__).DIRECTORY_SEPARATOR."Schedule.php";
class Notification {
	private $obj;
	
	//Template fields
	private $subject;
	private $textTemplate;
	private $htmlTemplate;
	
	//Content fields
	private $textContent;
	private $htmlContent;
	
	private $toUser;
	private $fromUser;
	private $notificationType;
	
	private $tpl, $projectDir, $siteURL;
	
	public function setSiteURL($siteURL){
		$this->siteURL = $siteURL;
	}
	
	public function __construct($fromEmail, $notificationType, Schedule $obj) 
    {
		// set value for field
		$this->toUser = $obj->getMemberDomain();
		$this->fromUser = $fromEmail;
		$this->notificationType = $notificationType;
		// use oid generator generate $oid
		$this->obj = $obj;
		
	}
	
	public function hasNoTemplate() 
    {
		// return false if $subject is empty
		if(Tool::isEmpty($this->subject))
		{
			return false;
		}
		return true;
	}
	
	public function getTextContent()
    {
		return $this->textContent;
	}
	
	public function getHtmlContent() 
    {
		return $this->htmlContent;
	}
	
	public function getObj()
    {
		return $this->obj;
	}
	
	public function setTemplate($subject, $textTemplate, $htmlTemplate) 
    {
		// set the value to field
		$this->subject = $subject;
		$this->textTemplate = $textTemplate;
		$this->htmlTemplate = $htmlTemplate;
		$this->generateContent();
	}
	
	public static function getEmailDeliveryNotification($fromEmail, Schedule $schedule) 
    {
		$type = "delivery";
		$obj = $schedule;
		$o = new self($fromEmail, $type, $obj);
		return $o;
	}
	
	public static function getEmailCollectionNotification($fromEmail, Schedule $schedule) 
    {
		$type = "collection";
		$obj = $schedule;
		$o = new self($fromEmail, $type, $obj);
		return $o;
	}
	
	public function getType() {
		return $this->notificationType;
	}
	
	private function generateContent() 
    {
		$find_ar = array("/#NAME#/");
		$replace_ar = array($this->toUser->getClientName());
		
        //$link = null;// add $link = null to test
        
		
        $link = $this->siteURL."/index.php?func=order&opt=viewC&id=".$this->getObj()->getScheduleDomain()->getScheduleID();
		
		if($link != null)
        {
			array_push($find_ar,"/#LINK#/");
			array_push($replace_ar,$link);
		}
        
		$data = array();//add $data = empty array to test
		if(!is_array($data))
        {
			$tmp = $data;
			$data = array();
			$data['/#DATA#/'] = $tmp;
		}
        else 
        {
			foreach($data as $key => $value)
	        {
				$find_ar[] = $key;
				$replace_ar[] = $value;
			}
        }

		$this->textContent = preg_replace($find_ar, $replace_ar, $this->textTemplate);
		$this->htmlContent = preg_replace($find_ar, $replace_ar, $this->htmlTemplate);
		$this->textContent = preg_replace("/#[a-zA-z]{1,20}#/", "", $this->textContent);
		$this->htmlContent = preg_replace("/#[a-zA-z]{1,20}#/", "", $this->htmlContent);
	}
    
	public function getContent()
	{
		
	}
	
	public function getToEmail() 
    {
	  	return $this->toUser->getEmail();
	}
	
	public function getToUser() 
    {
	  	return $this->toUser;
	}
 
 	public function getFromEmail() 
    {
        // if fromUser is empty, return noreply@bidproject.com
  		$email = $this->fromUser;
  		if(Tool::isEmpty($email))
  		{
   			return "noreply@delivery-collection-system.com";
  		}
  		return $email;
 	}
 	
	public function getFromName() 
    {
   		return "Delivery Collection System";
 	}
 
 	public function getSubject() 
    {
  		return $this->subject;
 	}
}